﻿
function $get(str) {
    return document.getElementById(str);
}
function _(str) {
    return document.getElementsByTagName(str);
}

$(function () {

    $('.icheckboxStyle').iCheck({
        checkboxClass: 'icheckbox_flat',
        radioClass: 'iradio_flat'
    });
    $('select').each(function () {
        
            var select = $(this);

            if (select.attr("multiple") === "multiple") {
                return;
            }

        select.select2({ width: select.width() + 14 });
    });
    $('.input-group.date').datepicker({
        format: "yyyy-mm-dd",
        startDate: "1998-1-01",
        endDate: "2030-1-01",
        language: "zh-CN", autoclose: true
    });
    $('input[placeholder], textarea[placeholder]').placeholder();
});


function SelectAll(checkbox) {
    var table;
    var checkboxHead;
    if (checkbox == null) {
        checkboxHead = $("#SelectAllBox");
    }
    else {
        checkboxHead = $(checkbox);
    }
    table = checkboxHead.parents("table");

    var isChecked = checkboxHead[0].checked;
    table.find("tr").find("td:eq(0) input:eq(0)").each(function (index) {
        //if (index == 0) return;
        if (true == isChecked) {
            this.checked = true;
        }
        else {
            $(this).removeAttr("checked");
        }

        if (this.checked == true) {
            $(this).parent().parent().css("background-color", "rgb(249, 249, 249)");
        } else {
            $(this).parent().parent().css("background-color", "#ffffff");
        }

    });

}

function CheckSelected(tempControl, gridTable) {
    var isChecked = false;

    var table = $("#" + gridTable);
    table.find("tbody tr").find("td:eq(0) input:eq(0)").each(function () {
        if (true == this.checked) {
            isChecked = true;
            return;
        }
    });

    if (true == isChecked) {
        return true;
    }
    else {
        //alert('请选中一项');
        $().message("请选中一项！");
        return false;
    }
}



function NewTabPage(title, url) {
    parent.NewTabPage(title, url);
    return false;
}



/*
实现Request.QueryString效果
*/
Request =
{
    QueryString: function (item) {
        var svalue = location.search.match(new RegExp("[\?\&]" + item + "=([^\&]*)(\&?)", "i"));
        return svalue ? svalue[1] : svalue;
    }
};

/*
重写验证控件的JS
*/
function ValidatorUpdateDisplay(val) {
    if (typeof (val.display) == "string") {
        if (val.display == "None") {
            return;
        }
        if (val.display == "Dynamic") {
            //val.style.display = val.isvalid ? "none" : "inline";
            // return;
        }
    }
    if ((navigator.userAgent.indexOf("Mac") > -1) &&
        (navigator.userAgent.indexOf("MSIE") > -1)) {
        val.style.display = "inline";
    }
    alert(val.innerHTML);
    // val.style.visibility = val.isvalid ? "hidden" : "visible";
}


Date.prototype.format = function (format) {
    var o = {
        "M+": this.getMonth() + 1, //month
        "d+": this.getDate(),    //day
        "h+": this.getHours(),   //hour
        "m+": this.getMinutes(), //minute
        "s+": this.getSeconds(), //second
        "q+": Math.floor((this.getMonth() + 3) / 3),  //quarter
        "f+": this.getMilliseconds() //millisecond
    }
    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (var k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            var str;

            if (RegExp.$1.length == 1) {
                str = o[k];
            } else if (RegExp.$1.length == 2) {
                str = ("00" + o[k]).substr(("" + o[k]).length);
            } else {
                str = ("000" + o[k]).substr(("" + o[k]).length);
            }


            format = format.replace(RegExp.$1, str);
        }
    }
    return format;
};

function ConfigPageSize() {
    var x = document.documentElement.clientHeight - 170;
    x = x / 26;
    x = Math.floor(x);
    return x;
}
function ConfigPageList() {
    var x = document.documentElement.clientHeight - 170;
    x = x / 26;
    x = Math.floor(x);
    return [x - 5, x, x + 5, x + 10, x + 15, 50, 100, 200, 400];
}

Message = function (msg) {
    $().message(msg);
};

jQuery.extend(jQuery, {
    // jQuery UI alert弹出提示
    jqalert: function (text, title, fn) {
        var html =
        '<div class="dialog" id="dialog-message">' +
        '  <p>' +
        '    <span class="ui-icon ui-icon-circle-check" style="float: left; margin: 0 7px 0 0;"></span>' + text +
        '  </p>' +
        '</div>';
        return $(html).dialog({
            //autoOpen: false,
            resizable: false,
            modal: true,
            show: {
                effect: 'fade',
                duration: 300
            },
            title: title || "提示信息",
            buttons: {
                "确定": function () {
                    var dlg = $(this).dialog("close");
                    fn && fn.call(dlg);
                }
            }
        });
    },
    // jQuery UI alert弹出提示,一定间隔之后自动关闭
    jqtimeralert: function (text, title, fn, timerMax) {
        var dd = $(
        '<div class="dialog" id="dialog-message">' +
        '  <p>' +
        '    <span class="ui-icon ui-icon-circle-check" style="float: left; margin: 0 7px 0 0;"></span>' + text +
        '  </p>' +
        '</div>');
        dd[0].timerMax = timerMax || 3;
        return dd.dialog({
            //autoOpen: false,
            resizable: false,
            modal: true,
            show: {
                effect: 'fade',
                duration: 300
            },
            open: function (e, ui) {
                var me = this,
                  dlg = $(this),
                  btn = dlg.parent().find(".ui-button-text").text("确定(" + me.timerMax + ")");
                --me.timerMax;
                me.timer = window.setInterval(function () {
                    btn.text("确定(" + me.timerMax + ")");
                    if (me.timerMax-- <= 0) {
                        dlg.dialog("close");
                        fn && fn.call(dlg);
                        window.clearInterval(me.timer); // 时间到了清除计时器
                    }
                }, 1000);
            },
            title: title || "提示信息",
            buttons: {
                "确定": function () {
                    var dlg = $(this).dialog("close");
                    fn && fn.call(dlg);
                    window.clearInterval(this.timer); // 清除计时器
                }
            },
            close: function () {
                window.clearInterval(this.timer); // 清除计时器
            }
        });
    },
    // jQuery UI confirm弹出确认提示
    jqconfirm: function (text, title, fn1, fn2) {
        var html =
        '<div class="dialog" id="dialog-confirm">' +
        '  <p>' +
        '    <span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>' + text +
        '  </p>' +
        '</div>';
        return $(html).dialog({
            //autoOpen: false,
            resizable: false,
            modal: true,
            show: {
                effect: 'fade',
                duration: 300
            },
            title: title || "提示信息",
            buttons: {
                "确定": function () {
                    var dlg = $(this).dialog("close");
                    fn1 && fn1.call(dlg, true);
                },
                "取消": function () {
                    var dlg = $(this).dialog("close");
                    fn2 && fn2(dlg, false);
                }
            }
        });
    },
    // jQuery UI 弹出iframe窗口
    jqopen: function (url, options) {
        var html =
        '<div class="dialog" id="dialog-window" title="提示信息">' +
        ' <iframe src="' + url + '" frameBorder="0" style="border: 0; " scrolling="auto" width="100%" height="100%"></iframe>' +
        '</div>';
        return $(html).dialog($.extend({
            modal: true,
            closeOnEscape: false,
            draggable: false,
            resizable: false,
            close: function (event, ui) {
                $(this).dialog("destroy"); // 关闭时销毁
            }
        }, options));
    },
    // jQuery UI confirm提示
    confirm: function (evt, text, title) {
        evt = $.event.fix(evt);
        var me = evt.target;
        if (me.confirmResult) {
            me.confirmResult = false;
            return true;
        }
        jQuery.jqconfirm(text, title, function (e) {
            me.confirmResult = true;
            if (e) {
                if (me.href && $.trim(me.href).indexOf("javascript:") == 0) {
                    $.globalEval(me.href);
                    me.confirmResult = false;
                    return;
                }
                var t = me.type && me.type.toLowerCase();
                if (t && me.name && (t == "image" || t == "submit" || t == "button")) {
                    __doPostBack(me.name, "");
                    me.confirmResult = false;
                    return;
                }
                if (me.click) me.click(evt);
            }
            return false;
        });
        return false;
    }
});